<?php
$errors = '';
$email = isset($_GET['email'])?$_GET['email']:null;
if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
	$errors = 'invalid email format';	
}
$phone = isset($_GET['phone'])?$_GET['phone']:null;
if(!preg_match("/^[0-9]{10}$/",$phone)){
	if($errors!='')$errors .='<br/>';
	$errors .= 'invalid phone format';	
} 
$name = isset($_GET['name'])?$_GET['name']:null;
if(strlen(trim($name))<4){
	if($errors!='')$errors .='<br/>';
	$errors .= 'name must be at least 4 chars';	
}
$pass = isset($_GET['pass'])?$_GET['pass']:null;
if(strlen(trim($pass))<8){
	if($errors!='')$errors .='<br/>';
	$errors .= 'pass must be at least 4 chars';	
}
if($errors!=''){
	echo $errors;
	return;
}

require_once '../providerConstants.php';
$mysqli = new mysqli($dbHost,$dbUser,$dbPass,$db);
$tempToken = substr(hash('sha256',mt_rand()),0,50);
$sql = 'insert into provider_master(email,phone,name,pass,tempToken) values(?,?,?,?,?)';
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('sssss',$email,$phone,$name,$pass,$tempToken);
if($stmt->execute()){
	$count = $stmt->affected_rows;
	if($count!=1)echo 'database problems ';
	else{
/*      when running local, you'll have to comment out the mail, because you prolly can't send mail.
 *      to confirm the registration, construct http://localhost/appRoot/registerConfirm.php?t=".$tempToken
 *      and replace appRoot with your app folder location.  replace $tempToken with the token in the database.
 */
//		mail($email,"confirmed","click on this http://provider.rostego.com/registerConfirm.php?t=".$tempToken);
		echo '0';
	}
}else{
	echo $mysqli->errno;
}
?>
